package com.cskaoyan.controller.wxcontroller;

/**
 * @Author Gan
 * @Date 2021/8/15 14:45
 * @Description: 卖票，五个卖票窗口，共100张票
 */
public class SellTicket implements Runnable {
    //售卖100张票
    private static int ticketNum = 100;

    @Override
    public void run() {
        while (true) {
            //sellTicket();
            synchronized (SellTicket.class) {
                if (ticketNum > 0) {
                    try {
                        Thread.sleep(70);
                        System.out.println("模拟业务话费一段时间~");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在出售第" + ticketNum + "张票");
                    ticketNum--;

                }
            }
            if (ticketNum == 0) {
                System.out.println("票卖完了！");
                return;
            }
        }
    }

    /**
     * 买票
     */

    private static synchronized void sellTicket() {
        if (ticketNum > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "正在出售第" + ticketNum + "张票");
            ticketNum--;
        }
    }

    public static class SellTicketDemo {
        public static void main(String[] args) {
            SellTicket sellTicket = new SellTicket();
            Thread t1 = new Thread(sellTicket, "窗口1");
            Thread t2 = new Thread(sellTicket, "窗口2");
            Thread t3 = new Thread(sellTicket, "窗口3");
            t1.start();
            t2.start();
            t3.start();
        }
    }


}
